//
//  LoigInViewController.m
//  SELF 豆瓣
//
//  Created by dllo on 15/11/8.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LoigInViewController.h"
#import "loginView.h"
#import "zhuCeViewController.h"
#import "DataBaseSingleton.h"
#import "user.h"
@interface LoigInViewController ()<loginDelegate>

@property(nonatomic,retain)loginView *myloginView;
@property(nonatomic,assign)BOOL isin;
@end

@implementation LoigInViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title= @"用户登录";
    //设置左侧按钮
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftButton:)]autorelease];
    self.myloginView = [[loginView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    [self.view addSubview:self.myloginView];
    [_myloginView release];
    self.myloginView.zhuCeDelegate = self;
    [self.myloginView.loginBtn addTarget:self action:@selector(loginBtn:) forControlEvents:UIControlEventTouchUpInside];
    self.myloginView.loginBtn.selected = NO;
    
    self.isin = NO;
    
    
    
    
}



//登录按钮
-(void)loginBtn:(UIButton *)btn
{
    NSString *name = self.myloginView.loginText.text;
    NSString *pass = self.myloginView.passWordText.text;
    NSMutableArray *usrArray = [NSMutableArray array];
    if (name.length == 0 || pass.length == 0) {
        UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:@"用户名或密码为空" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [actionsheet showInView:self.view.window];
        [actionsheet release];
        
    }
    
    
    else{
        
        
        //开建数据库
        [[DataBaseSingleton shareDataBase]openDB];
        //查询
        
        usrArray =  [[DataBaseSingleton shareDataBase]selectUser];
        user *myuer = [[user alloc]init];
        
        
        
        
        for (user *owner in usrArray) {
            if ([name isEqualToString:owner.userName])
            {
                myuer = owner;
                
                self.isin = YES;
                
                //判断是否登录成功 如果成功回到我的  页面
                if ([pass isEqualToString:myuer.password]) {
                    NSUserDefaults *uerDefault = [NSUserDefaults standardUserDefaults];
                    
                    [uerDefault setObject:@"1" forKey:@"isLogin"];
                    [uerDefault setObject:name forKey:@"name"];
                    
                    [uerDefault synchronize];
                    
                    [self.navigationController dismissViewControllerAnimated:YES completion:^{
                        
                        
                        
                    }];
                }
                else
                {
                    
                    UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:@"密码错误" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:@"其他", nil];
                    [actionsheet showInView:self.view.window];
                    [actionsheet release];
                    
                }
                
                
                
                
            }
            
        }
        
        if (self.isin == NO) {
            UIActionSheet *actionsheet = [[UIActionSheet alloc]initWithTitle:@"你还没有注册过" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [actionsheet showInView:self.view.window];
            [actionsheet release];
        }
        
    }
    
}




//注册
-(void)pushZhuCe
{
    zhuCeViewController *zhuceVC = [[zhuCeViewController alloc]init];
    
    [self.navigationController pushViewController:zhuceVC animated:YES];
    
    
    
}


//左侧点击按钮
-(void)leftButton:(UIBarButtonItem *)btn
{
    
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
